package com.yidu.best.systemManagement.userControl.controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.yidu.best.systemManagement.userControl.domain.User;
import com.yidu.best.systemManagement.userControl.domain.UserVo;
import com.yidu.best.systemManagement.userControl.service.UserService;

/**
 * 用户控制器
 * @author 阳雄
 *DateTime 2020年1月8日09:31:08
 */
@Controller
@RequestMapping("/sys/")
public class UserController {
	@Autowired
   private	UserService userService;
	
	
	public UserController() {
		super();
	}


	public UserController(UserService userService) {
		super();
		this.userService = userService;
	}


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	/**
	 * 加载用户数据信息
	 * @return
	 */
	@RequestMapping(value="userList")
	public String userDateList() {
		return "sys/user/userList";
		
	}
	/**
	 * 查找所有用户信息
	 * @param userVo 用户对象
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="users",method=RequestMethod.GET)
	public Map<String, Object> doUserDate(UserVo userVo){
		Map<String, Object> userMap = userService.queryUserByCriteriaWithPage(userVo);
		return userMap;
	}
	/**
	 * 添加，修改用户
	 * @param user
	 * @return 用户对象
	 */
	@RequestMapping(value="save",method=RequestMethod.POST)
	@ResponseBody
	public int saveUser(User user) {
		int row = 0;
		if(user.getUserId()==null || "".equals(user.getUserId())) {
			row = userService.addUser(user);
		}else {
			user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
			row = userService.updateUser(user);
			
		}
		return row;
	}
	/**
	 * 删除用户
	 * @param userIdStr
	 * @return 用户对象
	 */
	@ResponseBody
	@RequestMapping(value="deleteUser",method=RequestMethod.DELETE)
	public int deleteUser(String userIdStr) {
		int deleteById = userService.deleteUser(userIdStr);
		return deleteById;
	}
	/**
	 * 修改用户状态 
	 * @param user
	 * @return 用户对象
	 */
	@RequestMapping(value="logicdelete",method=RequestMethod.POST)
	@ResponseBody
	public int logicdelete(User user) {
		int updateUser = userService.logicDelete(user);
		return updateUser;
	}
	/**
	 * 图片上传
	 * @param file
	 * @param request
	 * @return
	 */
	 @ResponseBody
	    @RequestMapping("upload")
	    public Map<String, Object> upload(MultipartFile file,HttpServletRequest request){

	        String prefix="";
	        String dateStr="";
	        //保存上传
	        OutputStream out = null;
	        InputStream fileInput=null;
	        try{
	            if(file!=null){
	                String originalName = file.getOriginalFilename();
	                prefix=originalName.substring(originalName.lastIndexOf(".")+1);
	                Date date = new Date();
	                String uuid = UUID.randomUUID()+"";
	                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	                dateStr = simpleDateFormat.format(date);
	                String filepath = request.getServletContext().getRealPath("static/images") +"/"+ dateStr+"/"+uuid+"." + prefix;
	               
	                File files=new File(filepath);
	                //打印查看上传路径
	                //System.out.println(filepath); 
	                if(!files.getParentFile().exists()){
	                    files.getParentFile().mkdirs();
	                }
	                file.transferTo(files);
	                Map<String,Object> map2=new HashMap<>();
	                Map<String,Object> map=new HashMap<>();
	                map.put("code",0);
	                map.put("msg","");
	                map.put("data",map2);
	                map2.put("src","static/images/"+ dateStr+"/"+uuid+"." + prefix);
	                return map;
	            }

	        }catch (Exception e){
	        }finally{
	            try {
	                if(out!=null){
	                    out.close();
	                }
	                if(fileInput!=null){
	                    fileInput.close();
	                }
	            } catch (IOException e) {
	            }
	        }
	        Map<String,Object> map=new HashMap<>();
	        map.put("code",1);
	        map.put("msg","");
	        return map;
	    }
	 /**
	  * 查询用户名是否重复
	  * @param userName
	  * @return
	  */
	 @RequestMapping(value="selectUserName",method=RequestMethod.POST)
	 @ResponseBody
	 public User selectUserName(String userName) {
		 User result = userService.selectUserName(userName);
		return result;
		 
	 }

}
